<template>
  <div class="app-container">
    <el-tabs type="border-card" @tab-click="handleClick">
      <el-tab-pane label="广告">
        <div class="filter-container">
          <el-input
            v-model="listQuery.name"
            placeholder="机构名称"
            style="width: 100px;"
            class="filter-item"
            @keyup.enter.native="handleFilter"
          />
          <!-- <el-date-picker
            v-model="times"
            class="filter-item"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width: 250px;"
            type="daterange"
            value-format="yyyy-MM-dd"
          /> -->
          <el-date-picker
            v-model="listQuery.startTime"
             class="filter-item"
             style="width: 150px;"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            :clearable='false'
            :editable='false'
          >
          </el-date-picker>
          <el-button
            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
          >
            搜索
          </el-button>
        </div>
        <el-table
          :key="tableKey"
          v-loading="listLoading"
          :data="tableData"
          border
          fit
          highlight-current-row
          style="width: 100%;"
        >
          <el-table-column label="日期" min-width="110" align="center">
            <template slot-scope="{ row }">
              <span>{{ row.date }}</span>
            </template>
          </el-table-column>
          <el-table-column label="机构名称" min-width="150" align="center">
            <template slot-scope="{ row }">
              <span>{{ row.pName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="计划名称" min-width="110" align="center">
            <template slot-scope="{ row }">
              <span>{{ row.plan_name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="进件成功数" min-width="90" align="center">
            <template slot-scope="{ row }">
              <span>{{ row.successNum }}</span>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="listQuery.current"
          :limit.sync="listQuery.size"
          @pagination="statisticalSep"
        />
      </el-tab-pane>
      <el-tab-pane label="保险">
        <div class="filter-container">
          <el-input
            v-model="listQuery1.name"
            placeholder="机构名称"
            style="width: 100px;"
            class="filter-item"
            @keyup.enter.native="handleFilter"
          />
          <!-- <el-date-picker
            v-model="times"
            class="filter-item"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width: 250px;"
            type="daterange"
            value-format="yyyy-MM-dd"
          /> -->
          <el-date-picker
            v-model="listQuery1.startTime"
             class="filter-item"
             style="width: 150px;"
            type="date"
            placeholder="选择日期"
           value-format="yyyy-MM-dd"
           :clearable='false'
           :editable='false'
          >
          </el-date-picker>
          <el-button
            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
          >
            搜索
          </el-button>
        </div>
        <el-table
          :key="tableKey"
          v-loading="listLoading"
          :data="tableData"
          border
          fit
          highlight-current-row
          style="width: 100%;"
        >
          <el-table-column label="日期" min-width="110" align="center">
            <template slot-scope="{ row }">
              <span>{{ row.date }}</span>
            </template>
          </el-table-column>
          <el-table-column label="机构名称" min-width="150" align="center">
            <template slot-scope="{ row }">
              <span>{{ row.pName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="计划名称" min-width="110" align="center">
            <template slot-scope="{ row }">
              <span>{{ row.plan_name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="进件成功数" min-width="90" align="center">
            <template slot-scope="{ row }">
              <span>{{ row.successNum }}</span>
            </template>
          </el-table-column>
          <el-table-column label="进件成功率" min-width="90" align="center">
            <template slot-scope="{ row }">
              <span>{{ row.successRate }}%</span>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="listQuery1.current"
          :limit.sync="listQuery1.size"
          @pagination="statisticalSep1"
        />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getHistorySumInto } from "@/api/advertising";
import waves from "@/directive/waves"; // waves directive
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
export default {
  name: "CountChannel",
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableData: [],
      dayData: [],
      total: 0,
      daytotal: 0,
      tableKey: 0,
      listQuery: {
        current: 1,
        endTime: "",
        name: "",
        size: 10,
        startTime: "",
        type: 1,
      },
      listQuery1: {
        current: 1,
        endTime: "",
        name: "",
        size: 10,
        startTime: "",
        type: 2,
      },
      times: [],
      dayId: 0,
      countChannelUsersNum: 0,
      countUvNum: 0,
      daydialogFormVisible: false,
      dialogPvVisible: false,
      optionsCopy: [],
      options: [],
      tabActive: "0",
      listLoading: false,
    };
  },
  created() {
    this.statisticalSep();
    this.defaultDate()
  },
  methods: {
    defaultDate() {
      //获取新的时间
      let date = new Date();
      //获取当前时间的年份转为字符串
      let year = date.getFullYear().toString();
      //获取月份，由于月份从0开始，此处要加1，判断是否小于10，如果是在字符串前面拼接'0'
      let month =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1).toString()
          : (date.getMonth() + 1).toString();
      //获取天，判断是否小于10，如果是在字符串前面拼接'0'
      let da =
        date.getDate() < 10
          ? "0" + date.getDate().toString()
          : date.getDate().toString(); 
      //字符串拼接，开始时间，结束时间
      let end = year + "-" + month + "-" + da; //当天
      this.listQuery.startTime=end
      this.listQuery1.startTime=end

    },
    handleClick(tab) {
      this.tabActive = tab.index;
      this.handleFilter();
    },

    // 获取列表
    async statisticalSep() {
      const res = await getHistorySumInto(this.listQuery);
      this.tableData = res.data.data.records;
      this.total = res.data.data.total;
    },
    async statisticalSep1() {
      const res = await getHistorySumInto(this.listQuery1);
      this.tableData = res.data.data.records;
      this.total = res.data.data.total;
    },
    // 搜索
    handleFilter() {
      if (this.tabActive == "0") {
        this.statisticalSep(this.listQuery);
      } else if (this.tabActive == "1") {
        this.statisticalSep1(this.listQuery1);
      }

      // if (this.times == null) {
      //   this.listQuery.endTime = "";
      //   this.listQuery.startTime = "";
      // } else if (this.times.length === 0) {
      //   this.listQuery.endTime = "";
      //   this.listQuery.startTime = "";
      // } else {
      //   this.listQuery.endTime = this.times[1];
      //   this.listQuery.startTime = this.times[0];
      // }
      // if (this.times == null) {
      //   this.listQuery1.endTime = "";
      //   this.listQuery1.startTime = "";
      // } else if (this.times.length === 0) {
      //   this.listQuery1.endTime = "";
      //   this.listQuery1.startTime = "";
      // } else {
      //   this.listQuery1.endTime = this.times[1];
      //   this.listQuery1.startTime = this.times[0];
      // }
    },
  },
};
</script>

<style scoped>
.cap {
  opacity: 0.5;
}
</style>
